package adventure.commands;

import adventure.*;
import colour.ColourUtil;
import player.*;

/**
 *
 * @author Michael Hanns
 *
 */
public class Follow extends AbstractCommand {
    
    public Follow(Adventure a) {
        super(a, new String[]{"FOLLOW"});
    }
    
    @Override
    public String validateAndExecute(Player player, String[] args) {
        if (args.length == 1) {
            return viewFollowing(player);
        } else if (args.length == 2 && args[1].equalsIgnoreCase("self")) {
            return followSelf(player);
        } else {
            if (((AdventureEdit) adv).getRoomManager().isLightPresent(
                    player.x(), player.y(), player.z())) {
                return follow(player, Util.consolidateArguments(args));
            } else {
                return "You cannot see anything in the darkness!";
            }
        }
    }
    
    private String follow(Player player, String target) {
        int x = player.x();
        int y = player.y();
        int z = player.z();
        
        int targetPlayer = rooms.getRoomPlayer(x, y, z, target);
        if (player.getID() == targetPlayer) {
            return "You cannot follow yourself!";
        } else if (targetPlayer > 0) {
            target = players.getOnlinePlayer(targetPlayer).getAliveName();
            if (players.getOnlinePlayer(targetPlayer).addToFollowers(player)) {
                out.broadcastToPlayer(players.getOnlinePlayer(targetPlayer),
                        ColourUtil.colourise(player.getAliveName(), ColourUtil.magenta) + " is now following you.");
                
                return "\nYou are now following "
                        + ColourUtil.colourise(target, ColourUtil.magenta) + ".";
            } else {
                return "\nYou are already following "
                        + ColourUtil.colourise(target, ColourUtil.magenta) + "!";
            }
        } else {
            return "You don't see any " + target + "!";
        }
        
    }
    
    private String stopFollowing(Player player, String target) {
        int x = player.x();
        int y = player.y();
        int z = player.z();
        
        int targetPlayer = rooms.getRoomPlayer(x, y, z, target);
        if (player.getID() == targetPlayer) {
            return "You cannot stop following yourself!";
        } else if (targetPlayer > 0) {
            target = players.getOnlinePlayer(targetPlayer).getName();
            if (players.getOnlinePlayer(targetPlayer).removeFromFollowers(player)) {
                out.broadcastToPlayer(players.getOnlinePlayer(targetPlayer),
                        ColourUtil.colourise(player.getAliveName(), ColourUtil.magenta)
                        + " has stopped following you.");
                
                return "\nYou have stopped following " + ColourUtil.colourise(target, ColourUtil.magenta)
                        + ".";
            } else {
                return "\nYou are not following "
                        + ColourUtil.colourise(target, ColourUtil.magenta) + "!";
            }
        } else {
            return "You don't see any " + target + "!";
        }
        
    }
    
    private String followSelf(Player player) {
        Player[] group = player.playersFollowing();
        
        String out = "";
        if (group.length > 0) {
            out = stopFollowing(player, group[0].getAliveName());
            for (int x = 1; x < group.length; x++) {
                out += "\n" + stopFollowing(player, group[x].getAliveName());
            }
            
            return out;
        } else {
            return "You are not following anyone!";
        }
    }
    
    private String viewFollowing(Player p) {
        /* Generates a table that looks like this:
         *   +-----------+
         *   | Following |
         *   +-------------------------------------------------------------+
         *   | Player                    Vocation                 Level    |
         *   +-------------------------------------------------------------+
         *   | <playername>              <playervocation>         <lvl>    |
         *   | <playername>              <playervocation>         <lvl>    |
         *   | <playername>              <playervocation>         <lvl>    |
         *   | ...                       ...                      ...      |
         *   +-------------------------------------------------------------+
         * 
         */

        // Get the players
        Player[] following = p.playersFollowing();


        // Generate the table
        String groupTab = "\n +-----------+\n | "
                + ColourUtil.colourise("Following", ColourUtil.green) + " |";
        
        String dividerHead = "\n +-----------+-------------------------------------------------+";
        String divider = "\n +-------------------------------------------------------------+";
        
        String header = "\n | " + ColourUtil.colourise(addWhiteSpace("Name", 25)
                + " " + addWhiteSpace("Vocation", 25)
                + " " + addWhiteSpace("Level", 7), ColourUtil.green) + " |";
        
        String playersTable = "\n | You are not following anyone.                               |";
        
        if (following.length > 0) {
            playersTable = "\n | " + ColourUtil.colourise(addWhiteSpace(following[0].getAliveName(), 25)
                    + " " + addWhiteSpace(following[0].getVocationName(), 25)
                    + " " + addWhiteSpace(following[0].getLevel() + "", 7), ColourUtil.magenta) + " |";
            
            
            for (int x = 1; x < following.length; x++) {
                playersTable += "\n | " + ColourUtil.colourise(addWhiteSpace(following[x].getAliveName(), 25)
                        + " " + addWhiteSpace(following[x].getVocationName(), 25)
                        + " " + addWhiteSpace(following[x].getLevel() + "", 7), ColourUtil.magenta) + " |";
                
            }
        }
        
        return groupTab + dividerHead + header + divider + playersTable + divider;
    }
    
    private String addWhiteSpace(String input, int maxLength) {
        if (input.length() == maxLength) {
            return input;
        } else if (input.length() < maxLength) {
            for (int x = input.length(); x < maxLength; x++) {
                input += " ";
            }
            
            return input;
        } else {
            return input.substring(0, maxLength - 2) + "..";
        }
    }
}
